* D:\E\replications\BJPS2020\resultsets\figure_3.do
	// was: C:\Dropbox\Rehm\Hacker\submission\syntax-files\yale_eu-silc_july2019_correlates-of-drops.do

* cd D:\E\replications\BJPS2020\
set more off
clear all
cap log close
log using ./resultsets/figure_3.txt, text replace
display "$S_TIME  $S_DATE"
about
ado dir


set more off

use year pid hhid ccode iso3c weight female age hx040 ///
	hx010 px010 ///
	rb250 pb050 pb080 pb150 py010g py021g py050g py080g py090g py100g py110g py120g py130g py140g *wave *file ///
	hb010 hb020 hb030 hy010 hy010_f hy010_i hy020 hy020_f hy020_i hy022 hy022_f hy022_i hy023 hy023_f hy023_i hy025 hy025_f hy040g hy090g hy130g hy050g hy060g hy070g hy080g hy110g hy120g hy130g hy140g hx040 hx050 hx090 hx100 ///
	pe020 pl110 pe040* pl050 pl051 pb190 pl030 pl031 pl020 pl025 pl040 ///
	pl060 pl140 pl160 pl170 pl180 ph010 ph020 ph030 ///
	pb030 rb070 rb080 yob month ///
	hb050 pl21* ///
	using ./rawdata/silc/licensed/eu-silc.dta, clear

keep if inlist(ccode,2,20,900,200,205,210,211,212,220,225,230,235,255,305,325,350,375,380,385,390,395)

d hb050 pl21*
* // merge in variable on month of interview:
* cap drop _merge
* merge 1:1 pid hb010 ///
* 		using ./rawdata/silc/licensed/eu-silc.dta ///
* 		, keepusing(hb050 pl21*)
* drop if _merge==2
* drop _merge

* CPI (Eurostat)
cap drop cpi*
drop year 
gen year=hb010
replace year=year-1 if !inlist(ccode,200,205)
cap drop _merge
merge m:1 ccode year using ./rawdata/silc/cpi_eurostat.dta
drop if _merge==2
drop _merge
drop year 
gen year=hb010
	
// "All variables are in € (EURO). For the countries not members of the euro area the conversion factor can be found in variables HX010 and PX010.
// Income data (euro) i. e. HY020 * HX010 = income data (national currency)."
// C:\Dropbox\Rehm\Data\EU-SILC\yale\documentation_jan2016\L-2013 DIFFERENCES BETWEEN DATACOLLECTED AND UDB 15-08-01.doc
// convert countries that DO NOT HAVE the Euroe at any point into NCU:

ds hy??? hy???? py????
di "`r(varlist)'"
local n "`r(varlist)'"
local m: list uniq n
foreach v in `m' {
	replace `v'=`v'*(hx010) if inlist(ccode,200,225,290,310,316,343,344,345,355,360,367,368,380,385,390,395,640)
	// these countries switch from NCU to Euros: inlist(ccode,317,338,349,352)
}

* HY020 = HY010 – HY120G – HY130G – HY140G. See p. 218 in C:\Dropbox\Rehm\Data\EU-SILC\yale\documentation\Guidelines Doc65_2011.pdf

// these are very likely wrong in terms of income variables:
drop if ccode==212 & year==2002
drop if ccode==230 & year==2003
drop if ccode==230 & year==2004 & wave==2005
drop if ccode==220 & year<=2004
drop if ccode==235 & year<=2005
drop if ccode==325 & year<=2005
drop if ccode==350 & year<=2005
drop if ccode==367 & year<=2005
	
label define rb250 ///
	/// Information or interview completed
	11 "information completed only from interview" /// 
	12 "information completed only from registers" /// 
	13 "information completed from both: interview and registers" /// 
	14 "information completed from full-record imputation" /// 
	/// Interview not completed, though contact made
	21 "individual unable to respond (illness, incapacity, etc) and no proxy possible" /// 
	22 "failed to return self-completed questionnaire" /// 
	23 "refusal to co-operate" /// 
	/// Individual not contacted because
	31 "person temporarily away and no proxy possible" /// 
	32 "no contact for other reasons" /// 
	/// Information or interview not completed
	33 "information not completed: reason unknown", modify
label val rb250 rb250

* income concepts
cap drop DI MI
clonevar DI=hy020
label var DI "HH-level disposable income (EU-SILC's hy020)"
clonevar MI=hy023
label var MI "HH-level market income (EU-SILC's hy023)"

* Market income 'by hand'
egen tmp_mi_p=rowtotal(py010g py050g py080g)
bys hhid year: egen tmp_mi_h1=total(tmp_mi_p)
egen tmp_mi_h2=rowtotal(hy040g hy080g hy090g hy110g)
gen mi=tmp_mi_h1+tmp_mi_h2
replace mi=mi-hy130g if hy130g<.

* Transfer income 'by hand'
egen tmp_ti_p=rowtotal(py090g py100g py110g py120g py130g py140g)
bys hhid year: egen tmp_ti_h1=total(tmp_ti_p)
egen tmp_ti_h2=rowtotal(hy050g hy060g hy070g)
gen ti=tmp_ti_h1+tmp_ti_h2

* Taxes 'by hand'
egen tx=rowtotal(hy120g hy130g hy140g) 

* Gross income GI 'by hand'
egen gi=rowtotal(mi ti)
label var gi "Gross income, hh-level"

* Disposable income DI 'by hand'
gen di=gi-tx
label var di "Disposable income, hh-level"

replace di=. if DI==.
replace mi=. if MI==.

* For France
ds *DI*
foreach v in `r(varlist)' {
	local c: subinstr local v "DI" "di", all
	di "`v' -> `c'"
	*replace `v'=`c' if ccode==220
	replace `c'=`v' if ccode==220
}

ds *MI*
foreach v in `r(varlist)' {
	local c: subinstr local v "MI" "mi", all
	di "`v' -> `c'"
	*replace `v'=`c' if ccode==220
	replace `c'=`v' if ccode==220
}	

* equivalize
clonevar HHsize=hx040
gen equivalence=sqrt(HHsize)
label var equivalence "Equivalence scale: sqrt(HHsize)"

set more off 
foreach v in mi di {
	cap drop `v'_e
	gen `v'_e=`v'/equivalence if `v'>0 
}

* top- and bottom-coding, then CPI
set more off
foreach v of varlist mi_e di_e {
	bys ccode year: egen p1_`v'=wpctile(`v'), p(1) weights(weight)
	bys ccode year: egen p99_`v'=wpctile(`v'), p(99) weights(weight)
	replace `v'=p1_`v' if `v'<p1_`v' // bottom-code at p1
	replace `v'=p99_`v' if `v'>p99_`v' & `v'<. // top-code at p99
	replace `v'=`v'/cpi2010 // express in 2010 NCU
}

* Income drops and ginis
cap drop ESI*
egen group=group(wave ccode year), label

set more off
foreach v in mi_e di_e {
	xtset pid year, yearly
	*egen gini_`v'_2560 = inequal(`v') if age>=25 & age<=60, by(group) weight(weight) index(gini)
	*egen gini_`v'_all = inequal(`v'), by(group) weight(weight) index(gini)	
	*cap drop d`v'
	*gen d1_`v'=d1.`v'/abs(l1.`v') if `v'<. & l1.`v'<.
	*label var d1_`v' "Growth in `v'"
	gen d`v'=d1.`v'/(abs(l1.`v'+`v')/2) if `v'<. & l1.`v'<. // arc changes
	label var d`v' "Arc change of `v'"
	gen ESI25_`v'=(d`v'<=-0.25) if d`v'<.
	gen ESI50_`v'=(d`v'<=-0.5) if d`v'<.
	gen ESI10_`v'=(d`v'<=-0.1) if d`v'<.
	gen ESI00_`v'=(d`v'<0) if d`v'<.
	*gen upESI25_`v'=(d`v'>=0.25) if d`v'<.
	*gen upESI50_`v'=(d`v'>=0.5) if d`v'<.
	*gen upESI10_`v'=(d`v'>=0.1) if d`v'<.
	*gen upESI00_`v'=(d`v'>0) if d`v'<.
	*gen gESI25_`v' =(d1_`v'<=-0.25) if d1_`v'<.
	*gen gESI50_`v' =(d1_`v'<=-0.5) if d1_`v'<.
	*gen gESI10_`v' =(d1_`v'<=-0.1) if d1_`v'<.
	*gen gESI00_`v' =(d1_`v'<0)   if d1_`v'<.
	*gen upgESI25_`v'=(d1_`v'>=0.25) if d1_`v'<.
	*gen upgESI50_`v'=(d1_`v'>=0.5)  if d1_`v'<.
	*gen upgESI10_`v'=(d1_`v'>=0.1)  if d1_`v'<.
	*gen upgESI00_`v'=(d1_`v'>0)   if d1_`v'<.
	
	if "`v'"=="mi_e" local foo "HH-level market income (by hand)"
	*if "`v'"=="MI_e" local foo "HH-level market income (EU-SILC's hy010)"
	if "`v'"=="di_e" local foo "HH-level disposable income (by hand)"
	*if "`v'"=="DI_e" local foo "HH-level disposable income (EU-SILC's hy023)"
	*label var gini_`v'_2560 "Gini of `foo', ages 25-60"
	*label var gini_`v'_all "Gini of `foo', ages all"
	foreach j in 00 10 25 50 {
		label var ESI`j'_`v' "`j'+% arc drop, `foo'"
		*label var upESI`j'_`v' "`j'+% arc gain, `foo'"
		*label var gESI`j'_`v' "`j'+% drop, `foo'"
		*label var upgESI`j'_`v' "`j'+% gain, `foo'"
	}
}

* use C:\Users\rehm.16\Desktop\tmp\silc_dec2017\eu_silc_arc4.dta, clear

cap drop *_MI_e* 
cap drop *_DI_e*
cap drop RR*MIDI*
cap drop RR*midi*

xtset pid year, yearly

// Education
label define isced ///
	0 	"pre-primary education" ///
	1 	"primary education" ///
	2 	"lower secondary education" ///
	3 	"(upper) secondary education" ///
	4 	"post-secondary non-tertiary education" ///
	5 	"1st & 2nd stage of tertiary education" ///
	100 "Primary education" ///
	200 "Lower secondary education" ///
	300 "Upper secondary education" ///
	340 "without distinction of direct access to tertiary education" ///
	342 "partial level completion and without direct access to tertiary education" ///
	343 "level completion, without direct access to tertiary education" ///
	344 "level completion, with direct access to tertiary education" ///
	350 "without distinction of direct access to tertiary education" ///
	352 "partial level completion and without direct access to tertiary education" ///
	353 "level completion, without direct access to tertiary education" ///
	354 "level completion, with direct access to tertiary education" ///
	400 "Post-secondary non-tertiary education (not further specified)" ///
	440 "General education" ///
	450 "Vocational education" ///
	500 "Short cycle tertiary", modify

label val pe040 isced
gen isced=real(substr(string(pe040),1,1))
label val isced isced

gen eduh=inlist(isced,5) if inrange(isced,1,5)
tab pe040 eduh, m


// Triggers - large sample approach

* divorced/separated/widowed (reference period: current)
numlabel `: val label pb190', add
tab pb190
gen nospouseS=(inlist(pb190,3,4,5)) if inlist(pb190,1,2,3,4,5)
label var nospouseS "Divorced/separated/widowed (entire sample)"

// sickness (reference period: current)
foreach v in ph020 ph010 ph030 {
	numlabel `: val label `v'', add
	tab `v'
}
* feeling healthy -> feeling sick (reference period: current)
gen badhealthS=(inlist(ph010,4,5)) if inrange(ph010,1,5)
label var badhealthS "Sick (bad/very bad health)"

// change in HH size (reference period: income)
gen fambig=(hx040>l1.hx040) if hx040<. & l1.hx040<.
gen famsmall=(hx040<l1.hx040) if hx040<. & l1.hx040<.

* Employment status (reference period: income)
foreach v in a b c d e f g h i j k l {
	// unemployed
	gen     unemplS_`v'=.
	replace unemplS_`v'= (inlist(pl211`v',5)) if inrange(pl211`v',1,11)
	replace unemplS_`v'= (inlist(pl210`v',5)) if inrange(pl210`v',1,9)  & unemplS_`v'==.
}

egen unemplS_total=rowtotal(unemplS_*)
gen unemplS01=(unemplS_total>=1) if inrange(unemplS_total,0,12)
label var unemplS01 "Unemployed (at least 1m) (entire sample)"
gen unemplS66=(inrange(unemplS_total,6,12)) if inrange(unemplS_total,0,12)
label var unemplS66 "Unemployed (6-12m) (entire sample)"

foreach v in ///
	nospouseS ///
	badhealthS /// 
	fambig famsmall ///
	unemplS66 unemplS01 { 
	gen l1_`v'=l1.`v'
	egen g_`v'=group(l1_`v' `v'), label
	label var g_`v' "0 Not, 1 `: var label `v''"
	label list g_`v'
	gen to`v'=(g_`v'==2) if inlist(g_`v',1,2,3,4)
	label var to`v' "Not -> `: var label `v'' (01 vs. 00/10/11)"
	gen to`v'N=(g_`v'==2) if inlist(g_`v',1,2)
	label var to`v'N "Not -> `: var label `v'' (01 vs. 00)"
}

label var tonospouseS "Not married/married -> divorced/separated/widowed (entire sample, 01 vs. 00/10/11)" 
label var tobadhealthS "Healthy -> Sick (bad/very bad health) (entire sample, 01 vs. 00/10/11)"
label var tofambig "Increase in HH size"
label var tofamsmall "Decrease in HH size"

// Code trigger for someone else in HH
bys ccode hhid year: gen N_hhid=_N

foreach v in tounemplS01 tounemplS66 tobadhealthS { 
	cap drop total_`v'
	bys ccode hhid year: egen total_`v'=total(`v') if `v'<.
	cap drop t2_`v'
	gen t2_`v'= total_`v'-`v'
	cap drop `v'HH
	gen `v'HH=(t2_`v'>=1) if t2_`v'<.
}

// Try to match the reference periods
// Ameliorate the reference period problem by shifting the trigger (as opposed to shifting the shock)
xtset pid year, yearly
tab hb050
foreach rhs in tonospouseS tobadhealthS tobadhealthSHH {
	cap drop R`rhs'
	clonevar R`rhs'=`rhs'
	replace R`rhs'=l1.`rhs' if inlist(hb050,3,4) & !inlist(ccode,200,205)
	replace R`rhs'=l1.`rhs' if inlist(hb050,1,2) & ccode==200
}

label var tounemplS01     "-> Unemployed (1m+)"	 
label var tounemplS01HH   "-> HH Unemployed (1m+)" 
label var tounemplS66     "-> Unemployed (6m+)"  
label var tounemplS66HH   "-> HH Unemployed (6m+)" 
label var RtonospouseS    "-> Loss of spouse/partner" 
label var tofamsmall      "-> Smaller family"
label var tofambig        "-> Bigger family"
label var RtobadhealthS   "-> Bad health"
label var RtobadhealthSHH "-> HH Bad health"

// make sure these events are not constant in a country-year
foreach rhs of varlist tounemplS66 tounemplS66HH tounemplS01 tounemplS01HH RtonospouseS RtobadhealthS RtobadhealthSHH tofambig tofamsmall {
	di "`rhs'"
	bys ccode year: egen sd_`rhs'=sd(`rhs')
	tab ccode year if  sd_`rhs'==0
	replace `rhs'=. if sd_`rhs'==0 | sd_`rhs'==.
}
// Incidence rates, by country
tabstat tounemplS66 tounemplS66HH tounemplS01 tounemplS01HH RtonospouseS RtobadhealthS RtobadhealthSHH tofambig tofamsmall [aw=weight], by(ccode) s(mean) f(%5.4f)

// Incidence rates, low and high education, pooling country-years
foreach v in ///
	tounemplS66     ///
	tounemplS66HH   ///
	tounemplS01     ///
	tounemplS01HH   ///
	RtonospouseS    ///
	RtobadhealthSHH ///
	RtobadhealthS   ///
	RtobadhealthSHH ///
	{
	cap drop avgedu_`v'
	bys eduh: egen avgedu_`v'=wtmean(`v') if inrange(age,25,60), weight(weight)
	label var avgedu_`v' "Incidence (education): `: var label `v''"
	sum avgedu_`v' if eduh==0 [aw=weight]
	local m0 =round(`r(mean)',0.001)*100
	di "`m0'"
	local m0 `=substr("`m0'", 1, 3)'
	di "`m0'"
	sum avgedu_`v' if eduh==1 [aw=weight]
	local m1 =round(`r(mean)',0.001)*100
	local m1 `=substr("`m1'", 1, 3)'
	di "`v' -> `m'"
	label define `v' 1 "`: var label `v'' [L: `m0'%, H: `m1'%]", modify
	label val `v' `v'
}

foreach j in 10 25 50 {
	label var ESI`j'_mi_e "`j'+% arc drop, HH-level market income"
	label var ESI`j'_di_e "`j'+% arc drop, HH-level disposable income"
}

// Odds-ratio, low and high education
estimates clear
foreach j in 10 25 50 {
	foreach rhs in ///
		tounemplS01   ///
		tounemplS01HH ///
		tounemplS66   ///
		tounemplS66HH ///
		RtonospouseS  ///
		RtobadhealthS ///
		RtobadhealthSHH {
		
		di "*** `rhs' ***"
	
		* all countries pooled, by education
		eststo WMI`j'e0_`rhs': logit ESI`j'_mi_e i.`rhs' i.ccode i.year if inrange(age,25,60) & eduh==0 [pw=weight], or
		eststo WMI`j'e1_`rhs': logit ESI`j'_mi_e i.`rhs' i.ccode i.year if inrange(age,25,60) & eduh==1 [pw=weight], or
		eststo WDI`j'e0_`rhs': logit ESI`j'_di_e i.`rhs' i.ccode i.year if inrange(age,25,60) & eduh==0 [pw=weight], or
		eststo WDI`j'e1_`rhs': logit ESI`j'_di_e i.`rhs' i.ccode i.year if inrange(age,25,60) & eduh==1 [pw=weight], or
	}
}

// Saving the estimates
foreach j in 10 25 50 {
	estout ///
		WMI`j'e0_tounemplS66     ///
		WMI`j'e0_tounemplS66HH   ///
		WMI`j'e0_tounemplS01     ///
		WMI`j'e0_tounemplS01HH   ///
		WMI`j'e0_RtonospouseS    ///
		WMI`j'e0_RtobadhealthS   ///
		WMI`j'e0_RtobadhealthSHH ///
		WDI`j'e0_tounemplS66     ///
		WDI`j'e0_tounemplS66HH   ///
		WDI`j'e0_tounemplS01     ///
		WDI`j'e0_tounemplS01HH   ///
		WDI`j'e0_RtonospouseS    ///
		WDI`j'e0_RtobadhealthS   ///
		WDI`j'e0_RtobadhealthSHH ///
		WMI`j'e1_tounemplS66     ///
		WMI`j'e1_tounemplS66HH   ///
		WMI`j'e1_tounemplS01     ///
		WMI`j'e1_tounemplS01HH   ///
		WMI`j'e1_RtonospouseS    ///
		WMI`j'e1_RtobadhealthS   ///
		WMI`j'e1_RtobadhealthSHH ///
		WDI`j'e1_tounemplS66     ///
		WDI`j'e1_tounemplS66HH   ///
		WDI`j'e1_tounemplS01     ///
		WDI`j'e1_tounemplS01HH   ///
		WDI`j'e1_RtonospouseS    ///
		WDI`j'e1_RtobadhealthS   ///
		WDI`j'e1_RtobadhealthSHH ///
		using ./resultsets/figures/figure3_oddsratios`j'.txt ///
		, replace ///
		eform ///
		cells(b(star fmt(3) label(Coef)) se(par label(SE)) ci(par label(CI)))  ///
			stats(N r2_a r2_p chi2 ll df_m, fmt(0 3 3 1 1 1) labels("N. of cases" "adj R2" "Pseudo R2" "Wald Chi2" "Log likelihood" "DF")) ///
			legend varlabels(_cons Constant_`dv') label ///
			starlevels(# 0.1 * 0.05 ** 0.01) 
}
				
**************
*** Figure 3
**************
foreach j in 10 25 50 {
	coefplot ///
	   ( ///
		WMI`j'e0_tounemplS66     ///
		WMI`j'e0_tounemplS66HH   ///
		WMI`j'e0_tounemplS01     ///
		WMI`j'e0_tounemplS01HH   ///
		WMI`j'e0_RtonospouseS    ///
		WMI`j'e0_RtobadhealthS   ///
		WMI`j'e0_RtobadhealthSHH, label(Low education: MI) offset(0.2) ms(S)) ///
	   ( ///
		WDI`j'e0_tounemplS66     ///
		WDI`j'e0_tounemplS66HH   ///
		WDI`j'e0_tounemplS01     ///
		WDI`j'e0_tounemplS01HH   ///
		WDI`j'e0_RtonospouseS    ///
		WDI`j'e0_RtobadhealthS   ///
		WDI`j'e0_RtobadhealthSHH, label(DI) offset(0.1) ms(Sh)) ///
		( ///
		WMI`j'e1_tounemplS66     ///
		WMI`j'e1_tounemplS66HH   ///
		WMI`j'e1_tounemplS01     ///
		WMI`j'e1_tounemplS01HH   ///
		WMI`j'e1_RtonospouseS    ///
		WMI`j'e1_RtobadhealthS   ///
		WMI`j'e1_RtobadhealthSHH, label(High education: MI) offset(-0.1) ms(O)) ///
		( ///
		WDI`j'e1_tounemplS66     ///
		WDI`j'e1_tounemplS66HH   ///
		WDI`j'e1_tounemplS01     ///
		WDI`j'e1_tounemplS01HH   ///
		WDI`j'e1_RtonospouseS    ///
		WDI`j'e1_RtobadhealthS   ///
		WDI`j'e1_RtobadhealthSHH, label(DI) offset(-0.2) ms(Oh)) ///
		, eform drop(_cons *ccode* *year*) xline(1, lp(solid)) omitted /// 
		xscale(log range(.8 4)) /// xlabel(.8 ".25" .5 ".5" 1 "1" 2 "2") ///
		/// xlabel(0.8 1 1.5 2(1)4, angle(0)) ///
		/// xlabel(0 1(0.5)4, angle(0)) ///
		/// xscale(log range(.8 7)) /// xlabel(.8 ".25" .5 ".5" 1 "1" 2 "2") ///
		xlabel(0.75 1 1.5 2(1)6, angle(0) labsize(vsmall)) ///
		legend(pos(1) c(2) size(vsmall) region(lwidth(none))) /// yline(3.5 4.5) ///
		xtitle(" " "Odds ratio" "`j'%+ MI or DI drop", justification(centre) size(small)) ///
		ysize(6) xsize(5.5) ///
		/// title("Transitions -> `j'%+ drops" " ") ///
		scheme(rbn1mono) ///
		ylabel(,labsize(vsmall)) // coeflabels(, labsize(tiny)) 	headings(, labsize(tiny))
	
	graph save   ./resultsets/figures/figure3_`j'.gph, replace
	graph export ./resultsets/figures/figure3_`j'.pdf, replace
	*graph export ./resultsets/figures/figure3_`j'.emf, replace
}	
 

// How many 25% mi income drops are associated with triggers?
foreach rhs in ///
 	tounemplS01      ///
	tounemplS01HH    ///
 	tounemplS66      ///
	tounemplS66HH    ///
	tofambig         ///
	RtonospouseS     ///
	tofamsmall       ///
	RtobadhealthS    ///
	RtobadhealthSHH {
	
	preserve
		local dv ESI25_mi_e
		di "`rhs' -> `dv'"
		replace ccode=99
		keep if inrange(age,25,60)
		keep `dv' `rhs' weight ccode
		keep if !missing(`dv', `rhs', weight)
		tab `dv' `rhs'
		foreach v of varlist _all {
			local l`v': var label `v'
		}
		collapse (sum) weight, by(`dv' `rhs' ccode)
		foreach v of varlist _all {
			label var `v' "`l`v''"
		}
		rename `dv' ESI25_mi_
		rename `rhs' to_
		gen trigger="`rhs'"
		list
		save ./resultsets/figures/tmp/ESI_2x2_`rhs'.dta, replace
	restore
}

preserve
	clear
	foreach rhs in ///
		tounemplS01      ///
		tounemplS01HH    ///
		tounemplS66      ///
		tounemplS66HH    ///
		tofambig         ///
		RtonospouseS     ///
		tofamsmall       ///
		RtobadhealthS    ///
		RtobadhealthSHH {
		
		append using ./resultsets/figures/tmp/ESI_2x2_`rhs'.dta
	}
	drop ccode
	keep if ESI25_mi_==1
	drop ESI25_mi_
	reshape wide weight, i(trigger) j(to_)
	gen QOI=((weight1)/(weight0+weight1))*100
		// Quantiti Of Interest
	list trigger QOI
	format QOI %2.1f
	
	gen t2=""
	replace t2= "-> Unemployed (1m+)"	       if trigger=="tounemplS01"
	replace t2= "-> HH Unemployed (1m+)"       if trigger=="tounemplS01HH"  
	replace t2= "-> Unemployed (6m+)"          if trigger=="tounemplS66"
	replace t2= "-> HH Unemployed (6m+)"       if trigger=="tounemplS66HH"  
	replace t2= "-> Loss of spouse/partner"    if trigger=="RtonospouseS"
	replace t2= "-> HH Loss of spouse/partner" if trigger=="RtonospouseSHH" 
	replace t2= "-> Smaller family"            if trigger=="tofamsmall"     
	replace t2= "-> Bigger family"             if trigger=="tofambig"
	replace t2= "-> Bad health"                if trigger=="RtobadhealthS"
	replace t2= "-> HH Bad health"             if trigger=="RtobadhealthSHH"
	
	gsort -QOI
	*browse QOI t2 
	
	list QOI t2 
	sum QOI
	di "Sum of QOI: `=`r(mean)'*`r(N)''"
	sum QOI if trigger!="tounemplS01"
	di "Sum of QOI (without tounemplS01): `=`r(mean)'*`r(N)''"
	
	note: Created on `= c(current_date)'
	note: ./resultsets/figure_3.do
	note: ./resultsets/figures/figure3_triggers2x2.dta
	note: Association between triggers and 25% MI income drops
	saveold ./resultsets/figures/figure3_triggers2x2.dta, replace
	
restore

display "$S_TIME  $S_DATE"
cap log close

